#include <iostream>
#include <set>
#include <vector>
#include <utility>

using std::cout;
using std::endl;
using std::multiset;
using std::vector;
using std::pair;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    //multiset的特征
    //1、存放的也是key值，key值是不唯一的，可以重复
    //2、默认会按照key值升序排列
    //3、底层实现也是红黑树
    multiset<int> number{1, 3, 5, 8, 9, 4, 5, 6, 5, 3};
    /* multiset<int, std::greater<int>> number{1, 3, 5, 8, 9, 4, 5, 6, 5, 3}; */
    display(number);

    cout << endl << "multiset的查找操作" << endl;
    size_t cnt = number.count(5);
    cout << "cnt = " << cnt << endl;

    auto it = number.find(7);
    if(it == number.end())
    {
        cout << "该元素不存在multiset中" << endl;
    }
    else
    {
        cout << "该元素存在multiset中 " << *it << endl;
    }

    cout << endl << "测试multiset的bound函数" << endl;
    auto it2 = number.lower_bound(5);
    cout << "*it2 = " << *it2 << endl;

    auto it3 = number.upper_bound(5);
    cout << "*it3 = " << *it3 << endl;

    while(it2 != it3)
    {
        cout << *it2 << "  ";
        ++it2;
    }
    cout << endl;

    pair<multiset<int>::iterator, multiset<int>::iterator> 
        ret = number.equal_range(5);
    while(ret.first != ret.second)
    {
        cout << *ret.first << "   ";
        ++ret.first;
    }
    cout << endl;

    cout << endl << "multiset的insert操作" << endl;
    multiset<int>::iterator it8 = number.insert(100);
    display(number);

    cout << endl << endl;
    vector<int> vec = {11, 44, 99, 66};
    number.insert(vec.begin(), vec.end());
    display(number);

    cout << endl << endl;
    number.insert({7, 88, 44, 14, 56});
    display(number);

    cout << endl << "multiset的删除操作" << endl;
    it = number.end();
    --it;
    number.erase(it);
    display(number);

    //multiset没有下标访问运算符
    //不支持元素修改(红黑树)

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

